//
// Created by ta&to on 2022/8/16.
//
#include "http_connect.h"
void http_init(thread_pool_t *tpt, log_t *logt, http_socket_event_t *hset) {
    struct http_global_data_container hgdc;
    hset->hgdc = &hgdc;
    hset->hgdc->tpt = tpt;
    getcwd(hset->workpath, PATH_MAX);

    http_listen_start_io(hset);
}
void conn_init(thread_pool_t *tpt, log_t *logt, http_socket_event_t *hset, void* __socket_r_func, void* __socket_w_func){
    // 处理IO
    hset->logs = logt;
    for(int i = 0;i< sizeof hset->socket_fd /sizeof (struct http_server_socket_fd_s);i++) {
        hset->socket_fd[i].fd = 0;
    }
    for(int i=0;i<sizeof hset->client_fd / sizeof (struct http_client_socket_fd_s);i++){
        hset->client_fd[i].fd = 0;
    }

    hset->hgdefc->temp_func_handler->func_read = __socket_r_func;
    hset->hgdefc->temp_func_handler->func_write = __socket_w_func;
}

void init_listen(struct http_socket_event_s *hset) {
    int ports[] = {9527, 9528, 9529, 9530};
    for(int i = 0;i< sizeof(ports)/sizeof(int);i++) {
        hset->socket_fd[i].fd = CreateHttpSocket(hset->logs, 0, ports[i], 128);
    }

    for(int i = 0;i<sizeof(hset->socket_fd) / sizeof(struct http_server_socket_fd_s);i++) {
        hset->ev_container->maxfd = hset->ev_container->maxfd > hset->socket_fd[i].fd ? hset->ev_container->maxfd : hset->socket_fd[i].fd;
    }
}

int get_client_fd(struct http_socket_event_s *hset, http_socket_event_handle_t *handler, int fd) {
    hset->client_fd[0].fd = connAccept(hset->logs, handler, fd);
    return hset->client_fd[0].fd;
}

void unlock_client_fd(struct http_socket_event_s *hset, int fd){
    close(fd);
}
void init_conn_fd(struct http_socket_event_s *hset){
    for(int i = 0;i < MAX_CLINE;i++){
        const struct in_addr sin_addr;
        inet_aton("10.1.1.111",&sin_addr);

        hset->http_conn_fd[i].fd = CreateHttpClient(hset->logs, sin_addr, 8848);
        hset->http_conn_fd[i].can_use = 1;
        hset->http_conn_fd[i].used = 0;
        init_lock(&hset->http_conn_fd[i].use_lock);
    }
}
struct http_server_http_connect_fd_s random_conn_fd(struct http_socket_event_s *hset) {
    while (1) {
        for(int i = 0;i < MAX_CLINE;i++) {
            if(hset->http_conn_fd[i].can_use == 1 && hset->http_conn_fd[i].used == 0) {
                mutex_lock(&hset->http_conn_fd[i].use_lock);
                hset->http_conn_fd[i].used = 1;
                return hset->http_conn_fd[i];
            }
        }
    }
}

struct http_server_http_connect_fd_s reset_conn_fd(struct http_socket_event_s *hset, int fd) {
    close(fd);

    for(int i = 0;i < MAX_CLINE;i++) {
        if(hset->http_conn_fd[i].fd == fd) {
            const struct in_addr sin_addr;
            inet_aton("10.1.1.111",&sin_addr);
            hset->http_conn_fd[i].fd = CreateHttpClient(hset->logs, sin_addr, 8848);
            return hset->http_conn_fd[i];
        }
    }
}

void unlock_conn_fd(struct http_socket_event_s *hset, int fd) {
    for(int i = 0;i < MAX_CLINE;i++) {
        if(hset->http_conn_fd[i].fd == fd) {
            hset->http_conn_fd[i].used = 0;
            mutex_unlock(&hset->http_conn_fd[i].use_lock);
        }
    }
}
